<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item
        label="角色"
        prop="role"
      >
        <el-input
          v-model="queryParams.role"
          placeholder="请输入角色"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="卡号"
        prop="card"
      >
        <el-input
          v-model="queryParams.card"
          placeholder="请输入卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="姓名"
        prop="name"
      >
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item
        label="用户名"
        prop="username"
      >
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="密码"
        prop="password"
      >
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="地址"
        prop="address"
      >
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="部门"
        prop="department"
      >
        <el-input
          v-model="queryParams.department"
          placeholder="请输入部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="手机号"
        prop="telephone"
      >
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="待启用" value="0" />
          <el-option label="启用" value="1" />
          <el-option label="停用" value="2" />
        </el-select>
      </el-form-item>


      <el-form-item
        label="部门"
        prop="department"
      >
        <template>
          <el-select
            v-model="queryParams.department"
            collapse-tags
            style="margin-left: 20px;width:300px"
            placeholder="请选择部门"
          >
            <el-option
              v-for="item in options"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <!-- <el-form-item
        label="余额"
        prop="balance"
      >
        <el-input
          v-model="queryParams.balance"
          placeholder="请输入余额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="是否停运"
        prop="status"
      >
        <el-select
          v-model="queryParams.status"
          placeholder="请选择是否停运"
          clearable
          size="small"
        >
          <el-option
            label="请选择字典生成"
            value=""
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item
        label="微信openid"
        prop="openid"
      >
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入微信openid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="用户头像路径"
        prop="avatarUrl"
      >
        <el-input
          v-model="queryParams.avatarUrl"
          placeholder="请输入用户头像路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="是否需要修改密码"
        prop="needChangePassword"
      >
        <el-input
          v-model="queryParams.needChangePassword"
          placeholder="请输入是否需要修改密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item
        label="备注"
        prop="remarks"
      >
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:user:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:recharge:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:user:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="用户id"
        align="center"
        width="80"
        prop="id"
      />
      <el-table-column
        label="角色"
        align="center"
        width="80"
        prop="role"
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.role == 0 ? `普通用户` : `管理员` }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="卡号"
        align="center"
        show-overflow-tooltip
        prop="card"
      />
      <el-table-column
        label="姓名"
        align="center"
        show-overflow-tooltip
        prop="name"
      />
      <el-table-column
        label="用户名"
        align="center"
        show-overflow-tooltip
        prop="username"
      />
      <!-- <el-table-column
        label="密码"
        align="center"
        prop="password"
      /> -->
      <el-table-column
        label="地址"
        align="center"
        prop="address"
      />
      <el-table-column
        label="部门"
        align="center"
        prop="department"
      />
      <el-table-column
        label="手机号"
        show-overflow-tooltip
        align="center"
        prop="telephone"
      />
      <el-table-column
        label="余额"
        align="center"
        prop="balance"
      />
      <el-table-column
        label="状态"
        align="center"
        width="80"
        prop="status"
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.status == 0 ? `待启用` : scope.row.status == 1 ? `启用` : `停用` }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="是否长期"
        align="center"
        width="80"
        prop="status"
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.isLease == 0 ? `是` : `否` }}</el-tag>
        </template>
      </el-table-column>


      <el-table-column
        width="280px"
        label="微信openid"
        show-overflow-tooltip
        align="center"
        prop="openid"
      />
<!--      <el-table-column-->
<!--        label="用户头像路径"-->
<!--        align="center"-->
<!--        prop="avatarUrl"-->
<!--      >-->
<!--        <template slot-scope="scope">-->
<!--          <el-avatar-->
<!--            shape="square"-->
<!--            :size="50"-->
<!--            :src="scope.row.avatarUrl"-->
<!--          ></el-avatar>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="用户头像" align="center" prop="avatarUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>


      <el-table-column
        label="是否需要修改密码"
        align="center"
        prop="needChangePassword"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remarks"
      />
      <el-table-column
        label="操作"
        flex="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:recharge:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小程序端人员管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          label="角色"
          prop="role"
        >
          <el-select
            clearable
            v-model="form.role"
            placeholder="请输入角色"
            style="width: 100%"
          >
            <el-option
              v-for="o,i in [{label: `普通用户`, value: 0},{label: `管理员`, value: 1}]"
              :key="i"
              :label="o.label"
              :value="o.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="卡号"
          prop="card"
        >
          <el-input
            v-model="form.card"
            placeholder="请输入卡号"
          />
        </el-form-item>
        <el-form-item
          label="姓名"
          prop="name"
        >
          <el-input
            v-model="form.name"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item
          label="地址"
          prop="address"
        >
          <el-input
            v-model="form.address"
            placeholder="请输入地址"
          />
        </el-form-item>
        <el-form-item
          label="部门"
          prop="department"
        >
          <template>
            <el-select
              v-model="form.department"
              collapse-tags
              style="margin-left: 20px;width:300px"
              placeholder="请选择部门"
            >
              <el-option
                v-for="item in options"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="telephone"
        >
          <el-input
            v-model="form.telephone"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item
          label="余额"
          prop="balance"
        >
          <el-input
            v-model="form.balance"
            placeholder="请输入余额"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">待启用</el-radio>
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否长期" v-if="form.status==1" prop="isLease">
          <el-radio-group v-model="form.isLease">
            <el-radio :label="0">长期</el-radio>
            <el-radio :label="1">短期</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="日期" v-if="form.status==1 && form.isLease==1"  prop="leaseStartTime">
        <el-date-picker
          v-model="form.leaseStartTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :clearable=false
        >
        </el-date-picker>
        </el-form-item>

        <el-form-item
          label="微信openid"
          prop="openid"
        >
          <el-input
            v-model="form.openid"
            placeholder="请输入微信openid"
          />
        </el-form-item>
<!--        <el-form-item-->
<!--          label="用户头像路径"-->
<!--          prop="avatarUrl"-->
<!--        >-->
<!--          <el-input-->
<!--            v-model="form.avatarUrl"-->
<!--            placeholder="请输入用户头像路径"-->
<!--          />-->
<!--        </el-form-item>-->

        <el-form-item label="用户头像">
          <image-upload v-model="form.avatarUrl"/>
        </el-form-item>

        <el-form-item
          label="是否需要修改密码"
          prop="needChangePassword"
        >
          <el-input
            v-model="form.needChangePassword"
            placeholder="请输入是否需要修改密码"
          />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remarks"
        >
          <el-input
            v-model="form.remarks"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  exportUser,
} from "@/api/Recharge/user";

import { listDept } from "@/api/system/dept";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 小程序端人员管理表格数据
      userList: [],
      // 二级部门列表
      options: [],
      // 弹出层标题
      title: "",
      beginTimes:"",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        role: undefined,
        card: undefined,
        name: undefined,
        username: undefined,
        password: undefined,
        address: undefined,
        department: undefined,
        telephone: undefined,
        balance: undefined,
        status: undefined,
        openid: undefined,
        avatarUrl: undefined,
        needChangePassword: undefined,
        remarks: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        role: [
          {
            required: true,
            message: "角色，默认为0，0为普通用户，1为管理员不能为空",
            trigger: "blur",
          },
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        telephone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        department: [
          { required: true, message: `请选择部门`, trigger: "blur" },
        ],
        balance: [{ required: true, message: "余额不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "状态不能为空",
            trigger: "blur",
          },
        ],
        needChangePassword: [
          {
            required: true,
            message: "是否需要修改密码不能为空",
            trigger: "blur",
          },
        ],
        isLease:[
          {
            required: true,
            message: "请选择是否长期",
            trigger: "blur",
          },
        ],
        leaseStartTime:[
          {
            required: true,
            message: "日期不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getSecondDeptlist();
  },
  methods: {
    checkParament(val) {
      console.log(`val:`, val);
    },
    /** 获取二级部门列表 */
    getSecondDeptlist() {
      this.loading = true;
      listDept().then((response) => {
        console.log(`getSecondDeptlist:`, response.data);
        this.options = response.data;
      });
    },

    /** 查询小程序端人员管理列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.data.list;
        this.total = response.data.total;
        console.log(`userList:`, this.userList);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        role: undefined,
        card: undefined,
        name: undefined,
        username: undefined,
        password: undefined,
        address: undefined,
        department: undefined,
        telephone: undefined,
        balance: undefined,
        status: "0",
        openid: undefined,
        avatarUrl: undefined,
        createTime: undefined,
        updateTime: undefined,
        needChangePassword: undefined,
        remarks: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序端人员管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then((response) => {
        let { department } = response.data;
        console.log(`department:`, response.data);
        this.form = response.data;
        // this.form.department =
        //   department &&
        //   Array.isArray(department.split(",")) &&
        //   department.split(",").length
        //     ? department.split(",").map((o) => Number(o))
        //     : [];
        if(Number(response.data.department)==0){
          this.form.department=""
        }else {
        this.form.department=Number(response.data.department)
        }

        if(response.data.status==1 && response.data.isLease==1){
          let arr=[]
          arr.push(response.data.leaseStartTime)
          arr.push(response.data.leaseEndTime)
          this.form.leaseStartTime=arr
        }else {
          this.form.leaseStartTime=""
        }
        this.form.password=null,
        console.log(`leaseStartTime:`,  this.form.leaseStartTime);
        this.open = true;
        this.title = "修改小程序端人员管理";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {

          let { department } = this.form,
            _form = {};
          _form = Object.assign(
            { ...this.form }
            // { department: department.join(`,`) }
          );
          // debugger
          console.log(_form.leaseStartTime)
          if(_form.leaseStartTime!=null || _form.leaseStartTime!=undefined || _form.leaseStartTime!=undefined){
          _form.leaseEndTime=_form.leaseStartTime[1]
            _form.leaseStartTime=_form.leaseStartTime[0]
          }
          if (_form.id != undefined) {
            updateUser(_form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(_form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除小程序端人员管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有小程序端人员管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUser(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
  },
};
</script>
